package com.wyl.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyl.auth.entity.FullCity;
import com.wyl.auth.mapper.FullCityMapper;
import com.wyl.auth.req.FullCityReq;
import com.wyl.auth.service.FullCityService;
import com.wyl.auth.vo.FullCityVo;
import com.wyl.common.CommonPage;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @Description 省市区-字典
* @Author WuYiLong
* @Date 2024-11-24
*/
@Service
public class FullCityServiceImpl extends ServiceImpl<FullCityMapper, FullCity> implements FullCityService {

    @Override
    public CommonPage<FullCityVo> pageFullCity(FullCityReq fullCityReq) {
        Page<FullCity> page = new Page<>(fullCityReq.getCurrentPage(), fullCityReq.getPageSize());
        LambdaQueryWrapper<FullCity> wrapper = Wrappers.lambdaQuery(FullCity.class);
        Page<FullCity> fullCityPage = page(page, wrapper);
        if(fullCityPage.getRecords().isEmpty()) {
            return new CommonPage<>();
        }
        List<FullCity> fullCityList = fullCityPage.getRecords();
        List<FullCityVo> fullCityVos = fullCityList.stream().map(m -> {
            FullCityVo fullCityVo = new FullCityVo();
            BeanUtil.copyProperties(m, fullCityVo);
            return fullCityVo;
        }).collect(Collectors.toList());

        CommonPage<FullCityVo> commonPage = new CommonPage<>();
        commonPage.setCurrentPage(fullCityReq.getCurrentPage());
        commonPage.setPageSize(fullCityReq.getPageSize());
        commonPage.setList(fullCityVos);
        commonPage.setTotal((int)fullCityPage.getTotal());
        return commonPage;
    }
}
